<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/10/15
 * Time: 15:57
 */

class EventController extends Controller
{

    private $_logininfo;
    private $_daytime;
    private $_daydate;

    public function init()
    {
        parent::init();

        $this->_logininfo = $this->_session->offsetGet('loginInfos');

        $__now_time = $_SERVER['REQUEST_TIME'];
        $this->_daytime = date("Y-m-d h:i:s",$__now_time);
        $this->_daydate = date("Y-m-d",$__now_time);

    }

    public function indexAction()
    {

    }

    public function sampleAction(){
        global $_EventKeyworkCfg, $_SampleStateCfg;


        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();
            $Post['content'] = empty($Post['content']) ? '': htmlspecialchars($Post['content'],ENT_QUOTES,"UTF-8");
            $Post['conpany_name'] = empty($Post['conpany_name']) ? '': htmlspecialchars($Post['conpany_name'],ENT_QUOTES,"UTF-8");
            $Post['mobile'] = empty($Post['mobile']) ? '': htmlspecialchars($Post['mobile'],ENT_QUOTES,"UTF-8");
            $Post['username'] = empty($Post['username']) ? '': htmlspecialchars($Post['username'],ENT_QUOTES,"UTF-8");
            $Post['provice_id'] = empty($Post['provice_id']) ? '':  (int) $Post['provice_id'];
            $Post['city_id'] = empty($Post['city_id']) ? '': (int) $Post['city_id'];
            $Post['area_id'] = empty($Post['area_id']) ? '': (int) $Post['area_id'];
            $Post['detail_addr'] = empty($Post['detail_addr']) ? '': htmlspecialchars($Post['detail_addr'],ENT_QUOTES,"UTF-8");
            $Post['customer_remark'] = empty($Post['customer_remark']) ? '': htmlspecialchars($Post['customer_remark'],ENT_QUOTES,"UTF-8");
            $Post['product_id'] = empty($Post['product_id']) ? '':  (int) $Post['product_id'];
            $sample = new SampleModel();

            $data =array(
                'create_time'=> $this->_daytime,
                'create_uid'=>$this->_logininfo['id'],
                'update_time'=> $this->_daytime,
                'update_uid'=>'0',
                'user_id' => $this->_logininfo['id'],
                'sample_key'=>new Zend_Db_Expr('generatesampleKeyStr()'),
                'receive_business_name' => $Post['conpany_name'],
                'receive_mobile' => $Post['mobile'],
                'receive_name' => $Post['username'],
                'province_id' => $Post['provice_id'],
                'city_id' => $Post['city_id'],
                'area_id' => $Post['area_id'],
                'detail_addr' => $Post['detail_addr'],
                'ip' => Tools::getRemoteAddr(),
                'status' => $_SampleStateCfg['audit_delivery'],
                'customer_remark' => $Post['customer_remark'],
            );

            $data_sku = array(
                'create_time'=>$this->_daytime,
                'create_uid'=>$this->_logininfo['id'],
                'update_time'=>$this->_daytime,
                'update_uid'=>$this->_logininfo['id'],
                'sample_id'=>'',
                'product_id' => $Post['product_id'],
            );

            $sample = new SampleModel();
            $sample_id = $sample->add($data);

            if(!empty($sample_id)){
                $data_sku['sample_id'] =$sample_id;
                $samplesku = new SampleskuModel();
                $ret = $samplesku->add($data_sku);
            }

            if(!empty($ret)){
                $retdata = array(
                    'code' => 8,
                    'msg'  => '提交申请成功,等待审核!'
                );
                echo json_encode($retdata);
                die();
            }
            die();
        }


       
        $activity = new ActivityModel();
        $activitysku = new ActivityskuModel();

        $wheres = "name like'%{$_EventKeyworkCfg['sample']}%'";
        $activityinfo =$activity->getRowByWhere($wheres);

        $where = " activity.id ='{$activityinfo['id']}' and activity_sku.status = 'Y'";
        $activityskuinfo = $activitysku->getSampleList($where);


        $sampleorder = new SampleskuModel();

        if(!empty($this->_logininfo['id'])){
            foreach($activityskuinfo as $key=>$var){
                $isbuy = $sampleorder->getSampleOrder("free_sample.user_id = '{$this->_logininfo['id']}' and free_sample_sku.product_id ='{$var['product_sku_id']}'");
                if(empty($isbuy)){
                    $activityskuinfo[$key]['is_application'] = "Y";
                }else{
                    $activityskuinfo[$key]['is_application'] = "N";
                }
            }
        }else{
            foreach($activityskuinfo as $key=>$var){
                $activityskuinfo[$key]['is_application'] = "Y";
            }
        }



        $user = new UsersinvoiceModel();

        if(!empty($this->_logininfo)){
            $userinfo = $user->getRowByWhere("user_id ='{$this->_logininfo['id']}'");
        }else{
            $userinfo = array(
                'business_name'=>'',
                'contacts'=>'',
                'mobile'=>'',
            );
        }



        $region = new RegionModel();
        $province = $region->getListByRow("region_pid",0,"region_pid desc");
        $this->_view->province = $province;

        $this->_view->userinfo = $userinfo;
        $this->_view->logininfo = $this->_logininfo['id'];
        $this->_view->activityskuinfo = $activityskuinfo;

    }


}